package com.tywj.buscustomerapp.presenter;

import android.content.Context;
import android.util.Log;

import com.tywj.buscustomerapp.model.BuyTicketModel;
import com.tywj.buscustomerapp.model.bean.BuyedBean;
import com.tywj.buscustomerapp.model.bean.DateJoe;
import com.tywj.buscustomerapp.model.bean.StationBean;
import com.tywj.buscustomerapp.model.bean.YuPiaoBean;
import com.tywj.buscustomerapp.presenter.contract.BuyTicketContract;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BuyTicketPresenter implements BuyTicketContract.BuyTicketPresenter {
    private BuyTicketContract.BuyTicketModel model;
    private BuyTicketContract.BuyTicketView view;
    public BuyTicketPresenter(BuyTicketContract.BuyTicketView view) {
        this.view = view;
        this.model = new BuyTicketModel();
    }

    @Override
    public void startLoadStations(final String stationId) {
        Observable.create(new ObservableOnSubscribe<List<StationBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<StationBean>> e){
                e.onNext(model.loadStations(stationId));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<StationBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (view!=null) {
                            view.showLoading();
                        }
                    }

                    @Override
                    public void onNext(List<StationBean> s) {
                        if (view!=null) {
                            view.loadedData(s);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (view!=null) {
                            view.closeLoading();
                            view.loadError("线路加载失败");
                        }
                    }

                    @Override
                    public void onComplete() {
//                        if (view!=null) {
//                            view.closeLoading();
//                        }
                    }
                });
    }

    @Override
    public void startGetPrice(final String lineId, final String start, final String end) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e){
                e.onNext(model.getPiaoJia(lineId,start,end));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (view!=null)
                            view.showLoading();
                    }

                    @Override
                    public void onNext(String s) {
                        if (view!=null && s!=null)
                            view.loadeBuyPrice(s);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
//                        if (view!=null)
//                            view.closeLoading();
                    }
                });
    }

    @Override
    public void startCheckTicket(final int pos, final DateJoe time, final String lineId, final String start, final String end) {
        Log.i("taylor",time+","+lineId+","+start+","+end);
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e){
                e.onNext(model.checkIsCanBuyTicket(time.getTotalDate(),lineId,start,end));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        view.showLoading();
                    }

                    @Override
                    public void onNext(String s) {
                        if (s!=null && s.equals("true")){
                            startGetCont(pos,time,lineId,start,end);
                        }else{
                            if (view!=null) {
                                view.loadeIsCanBuyTicket("false", time, pos);
                                view.loadError("该车票已售罄");
                                view.closeLoading();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (view!=null)
                            view.closeLoading();
                    }

                    @Override
                    public void onComplete() {
//                        view.closeLoading();
                    }
                });
    }

    @Override
    public void startGetCont(final int pos, final DateJoe time, final String lineId, final String start, final String end) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e){
                e.onNext(model.getCont(time.getTotalDate(),lineId,start,end));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        view.showLoading();
                    }

                    @Override
                    public void onNext(String s) {
                        if (view!=null){
                            int x = Integer.valueOf(s);
                            if (x>0){
                                view.loadeIsCanBuyTicket1(x,time,pos,s);
                            }else{
                                view.loadeIsCanBuyTicket("false",time,pos);
                                view.loadError("该车票已售罄");
                                view.closeLoading();
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if (view!=null) {
                            view.closeLoading();
                        }
                    }
                });

    }

    @Override
    public void doBuyTicketOnOnce(Context context, String yhm, String ch, String gmqsz, String gmzdz, List<String> ccsjs,
                                  String scsj, String xcsj,int number) {
        if (view!=null) {
            view.showLoading();
            model.doBuyOnceTicket(view, context, yhm, ch, gmqsz, gmzdz, ccsjs, scsj, xcsj, number);
        }

//        Observable.create(new ObservableOnSubscribe<String>() {
//            @Override
//            public void subscribe(ObservableEmitter<String> e){
//                model.doBuyOnceTicket(context,phone,lineNumber,start,end,busTime,statTime,endTime);
////                e.onNext(model.doBuyTicketOnOnce(phone,lineNumber,start,end,userStart,userEnd,busTime,buyTIme,price,state,city,busNumber,statTime,endTime));
//                e.onComplete();
//            }
//        }).subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<String>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        view.showLoading();
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//                        view.loadeIsBuySuccess(s,isEnd);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        view.loadeIsBuySuccess("false",isEnd);
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });


    }




    @Override
    public void doBuyTicketOnMonth(Context context, final String phone, final String lineNumber, final String month, final String year, final String start, final String end,
                                   final String startTime, final String endTime) {
        model.doBuyMonthTicket(view,context,phone,lineNumber,month,year,start,end,startTime,endTime);
//        Observable.create(new ObservableOnSubscribe<String>() {
//            @Override
//            public void subscribe(ObservableEmitter<String> e){
//                String code = model.doCheckMonthTicket(year,month,lineNumber,start,end);
//                if (code!=null && code.equals("true")){
//                    e.onNext(model.doBuyMonthTicket(phone,lineNumber,month,year,start,end,startTime,endTime));
//                    e.onComplete();
//                }else{
//                    e.onError(null);
//                }
//            }
//        }).subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<String>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        view.showLoading();
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//                        view.loadBuyMonthSuccess(s);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        view.loadError("月票已售罄");
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        view.closeLoading();
//                    }
//                });
    }

    @Override
    public void getDays(final String lineNum) {
        Observable.create(new ObservableOnSubscribe<List<YuPiaoBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<YuPiaoBean>> e){
                List<YuPiaoBean> yuPiaoBeans = model.getDays(lineNum);
                if (yuPiaoBeans!=null && yuPiaoBeans.size()>0){
                    e.onNext(yuPiaoBeans);
                    e.onComplete();
                }else {
                    e.onError(null);
                }

            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<YuPiaoBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (view!=null)
                            view.showLoading();
                    }

                    @Override
                    public void onNext(List<YuPiaoBean> list) {
                        if (view!=null)
                            view.loadDays(list);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (view!=null)
                            view.loadError("连接失败");
                    }

                    @Override
                    public void onComplete() {
//                        if (view!=null)
//                            view.closeLoading();
                    }
                });
    }

    @Override
    public void getBuyeds(final String lineNum, final String phone) {
        Observable.create(new ObservableOnSubscribe<List<BuyedBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<BuyedBean>> e){
                List<BuyedBean> beans = model.getBuyeds(lineNum,phone);
                if (beans!=null && beans.size()>0){
                    e.onNext(beans);
                    e.onComplete();
                }else {
                    e.onError(null);
                }

            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<BuyedBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
//                        if (view!=null)
//                            view.showLoading();
                    }

                    @Override
                    public void onNext(List<BuyedBean> list) {
                        if (view!=null)
                            view.getBuyeds(list);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (view!=null) {
                            view.getBuyeds(null);
                            view.closeLoading();
                        }
                    }

                    @Override
                    public void onComplete() {
                        if (view!=null)
                            view.closeLoading();
                    }
                });
    }


    @Override
    public void onDestroy() {
        view = null;
        System.gc();
    }
}
